package com.examination.service.impl;

import com.examination.bean.User;
import com.examination.service.IUserService;
import com.examination.utils.DbUtil;

import java.sql.*;
import java.util.Map;
public class UserService implements IUserService {
    @Override
    public User getUser(Map map){
        Object account = map.get("name");
        Object role_type = map.get("role_type");
        User user = null;
        if(account != null){
            //数据库查询得到的
            Connection conn = DbUtil.getConnInstance();
            //3.操作数据库，实现增删改查 
            ResultSet rs = null;
            PreparedStatement stmt = null;
            try {
                String sql = "SELECT id,name,password FROM e_user where name = ? and role_type  = ?";
                stmt = conn.prepareStatement(sql);
                stmt.setString(1,account.toString());
                stmt.setString(2,role_type.toString());
                rs = stmt.executeQuery();
                if(rs.next()){
                    rs.previous();
                    user = new User();
                    while(rs.next()){
                        user.setId(rs.getLong("id"));
                        user.setName(rs.getString("name"));
                        user.setPassword(rs.getString("password"));
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return user;
    }
}
